package com.silence.gmall.product.controller;

import com.silence.gmall.annotations.Log;
import com.silence.gmall.base.Result;
import com.silence.gmall.common.vo.res.FileUploadResVo;
import com.silence.gmall.template.MinioTemplate;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Optional;

/**
 * @Author silence
 * @Date 2025-08-20 09:28
 * @Email 792245022@qq.com
 * @Version 1.0
 * @Description 文件控制层
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/product")
public class FileController {

    /**
     * Minio 文件模板接口
     */
    private final MinioTemplate minioTemplate;

    /**
     * 文件上传
     *
     * @param file 文件
     * @return 文件路径
     */
    @PostMapping("/fileUpload")
    @Log(interfaceName = "文件上传")
    public Result<String> fileUpload(@RequestParam("file") MultipartFile file) {
        return Result.build(Optional.ofNullable(minioTemplate.uploadFile(file))
                .map(FileUploadResVo::getFileUrl)
                .orElse(""));
    }
}
